<?php
/**
 *
 * @author henry
 * @version 
 */
require_once 'Zend/View/Interface.php';

/**
 * BaseUrl helper
 *
 * @uses viewHelper Zend_View_Helper
 */
class Zend_View_Helper_OutputCateCombobox {
	
	/**
	 * @var Zend_View_Interface 
	 */
	public $view;
	
	/**
	 *  $catString co the la pid cua mot phan loai khac neu trong truong hop load cho categories
         *  $id la id hien tai cua phan loai dang xu ly
	 */
	public function outputCateCombobox($data,$id,$catString = null) {
            $out = "<select name=\"pid\">
            <option value=\"0\">".$this->view->t('[Phan loai goc]')."</option>";
            $combobox = $this->renderComboboxToArray($data,$id);
            if($combobox != null){
                foreach ($combobox as $key=>$item){
                    $select = $key == $catString?"selected=\"selected\"":"";
                    $out .= "<option $select value=\"$key\">$item</option>";
                }
            }
            $out .= "</select>";
            return $out;
	}
        private function renderComboboxToArray($data,$id,$sysbol="--"){
            if (empty ($data)) return null;
            if (!is_array($data)) throw new Exception("Phai la Array trong outputCate");
            $result = array();

            foreach ($data as $key => $item){
                if ($item['id'] == $id) continue;
                $result[$item['id']] = $sysbol.$item['title'].$pid;
                $child = $this->renderComboboxToArray($item['child'],$id,$sysbol."--");
                if($child != null){
                    foreach ($child as $key => $itemchild){
                        $result[$key] = $itemchild;
                    }
                }
            }            
            return $result;
        }
	
	/**
	 * Sets the view field 
	 * @param $view Zend_View_Interface
	 */
	public function setView(Zend_View_Interface $view) {
		$this->view = $view;
	}
}
